package com.limeng.datastruts.stack;

import java.util.Arrays;

public class ArrayStack {

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack();
        stack.push(4);
        stack.push(5);
        stack.push(1);
        stack.push(6);
        stack.push(3);
        stack.push(2);

        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());

        System.out.println("size:" + stack.nums.length);
    }

    private int[] nums;
    private int count;

    private static int INIT_COUNT = 5;

    public ArrayStack() {
        count = 0;
        nums = new int[INIT_COUNT];
    }

    /**
     * 扩容
     *
     * @return
     */
    private boolean resize() {
        nums = Arrays.copyOf(nums, nums.length * 2);
        return true;
    }

    /**
     * 入栈
     *
     * @param num
     * @return
     */
    public boolean push(int num) {

        if (++count >= nums.length) {
            resize();
        }
        nums[count] = num;
        return true;
    }

    /**
     * 出栈
     *
     * @return
     */
    public int pop() {
        if (count == 0) {
            System.out.println("栈已为空");
            return 0;
        }
        int num = nums[count];
        count--;
        return num;
    }

}
